package com.manage.controller;

import com.manage.pojo.Orders;
import com.manage.pojo.operation.OrdersList;
import com.manage.service.OrdersService;
import com.manage.utils.FBJSONResult;
import com.manage.utils.PagedResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@Api(value = "有关订单的操作", tags = {"有关订单的操作"})
public class OrdersController {

    @Autowired
    private OrdersService ordersService;

    @ApiOperation(value = "订单信息添加", notes = "订单信息添加接口")
    @PostMapping("/order/orderRegist")
    public FBJSONResult orderRegist(@RequestBody Orders order) throws Exception {
        order.setOederContent(order.getOederContent());
        order.setOrderPrice(order.getOrderPrice());
        order.setUserId(order.getUserId());
        order.setShoesId(order.getShoesId());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:dd");
        order.setOrderTime(sdf.format(new Date()));
        order.setOrderState(order.getOrderState());
        order.setOrderAddress(order.getOrderAddress());
        order.setOrderPhone(order.getOrderPhone());
        order.setOrderName(order.getOrderName());
        order.setOrderShoesImage(order.getOrderShoesImage());
        ordersService.saveOrders(order);
        return FBJSONResult.ok(order);
    }

    @ApiOperation(value = "订单信息删除", notes = "订单信息删除接口")
    @ApiImplicitParam(name = "orderId", value = "订单id", required = true, dataType = "String", paramType = "query")
    @PostMapping("/order/orderDelete")
    public FBJSONResult orderDelete(String orderId) throws Exception {
        if(StringUtils.isBlank(orderId)) {
            return FBJSONResult.errorMsg("订单id不能为空");
        }
        boolean orderIdIsExit = ordersService.queryOrdersIsExit(orderId);
        if(orderIdIsExit) {
            ordersService.orderDelete(orderId);
        }
        return FBJSONResult.ok("删除成功");
    }

    @ApiOperation(value = "订单信息更加", notes = "订单信息添加接口")
    @PostMapping("/order/orderUpdate")
    public FBJSONResult orderUpdate(@RequestBody Orders order) throws Exception {
        boolean orderIdIsExit = ordersService.queryOrdersIsExit(order.getOrderId());
        if(orderIdIsExit) {
            Orders orderExample = new Orders();
            orderExample.setOrderId(order.getOrderId());
            orderExample.setOederContent(order.getOederContent());
            orderExample.setOrderPrice(order.getOrderPrice());
            orderExample.setUserId(order.getUserId());
            orderExample.setShoesId(order.getShoesId());
            orderExample.setOrderState(order.getOrderState());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            orderExample.setOrderTime(sdf.format(new Date()));
            orderExample.setOrderAddress(order.getOrderAddress());
            orderExample.setOrderPhone(order.getOrderPhone());
            orderExample.setOrderName(order.getOrderName());
            orderExample.setOrderShoesImage(order.getOrderShoesImage());
            ordersService.orderUpdate(orderExample);
            return FBJSONResult.ok(orderExample);
        }else {
            return FBJSONResult.errorMsg("更新失败");
        }
    }

    @ApiOperation(value = "订单信息查询(根据订单id)", notes = "订单信息查询接口(根据订单id)")
    @ApiImplicitParam(name = "orderId", value = "订单id", required = true, dataType = "String", paramType = "query")
    @PostMapping("/order/queryOrderInfoByOrderId")
    public FBJSONResult queryOrderInfoByOrderId(String orderId) throws Exception {
        if(StringUtils.isBlank(orderId)) {
            return FBJSONResult.errorMsg("订单id不能为空");
        }
        OrdersList orderResult = ordersService.queryOrdersInfoByOrderId(orderId);
        return FBJSONResult.ok(orderResult);
    }

    @ApiOperation(value = "订单信息列表查询(根据用户id)", notes = "订单信息列表查询(根据用户id)接口")
    @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "String", paramType = "query")
    @PostMapping("/order/queryOrdersInfoByUserId")
    public FBJSONResult queryOrdersInfoByUserId(Integer page, String userId) throws Exception {
        if(StringUtils.isBlank(userId)) {
            return FBJSONResult.errorMsg("用户id不能为空");
        }
        if(page == null) {
            page = 1;
        }
        PagedResult result = ordersService.queryOrdersInfoByUserId(page, 10, userId);
        return FBJSONResult.ok(result);
    }

    @ApiOperation(value = "订单信息列表查询(根据鞋id)", notes = "订单信息列表查询(根据鞋id)接口")
    @ApiImplicitParam(name = "shoesId", value = "鞋id", required = true, dataType = "String", paramType = "query")
    @PostMapping("/order/queryOrdersInfoByShoesId")
    public FBJSONResult queryOrdersInfoByShoesId(Integer page, String shoesId) throws Exception {
        if(StringUtils.isBlank(shoesId)) {
            return FBJSONResult.errorMsg("鞋id不能为空");
        }
        if(page == null) {
            page = 1;
        }
        PagedResult result = ordersService.queryOrdersInfoByShoesId(page, 10, shoesId);
        return FBJSONResult.ok(result);
    }

    @ApiOperation(value = "订单信息列表查询(所有订单)", notes = "订单信息列表查询(所有订单)接口")
    @PostMapping("/order/queryAllOrdersInfo")
    public FBJSONResult queryAllOrdersInfo(Integer page) throws Exception {
        if(page == null) {
            page = 1;
        }
        PagedResult result = ordersService.queryAllOrders(page, 10);
        return FBJSONResult.ok(result);
    }

}
